<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发送请求</title>
</head>
<body>
<button onclick="send4()">send</button>


<script src="../static/js/axios.js"></script>
<script src="../static/js/jquery.js"></script>
<script>
    console.log(Math.ceil(Math.random() * 39 + 1))
    // $.ajax({url, type, data, success, error})
    // $.get(url, data, callback)
    function send() {
        $.ajax({
            url: 'http://localhost:8080/get',
            type: 'GET',
            data: {
                username: "root"
            },
            success: (resp) => {
                console.log(resp)
            },
            error: (error) => {
                console.log(error)
            }
        })
    }

    function send2() {
        axios.get("http://localhost:8080/get").then((res) => {
            console.log(res)
            console.log(res.data)
        }).catch(err => {
            console.log(err)
        })
    }

    function send3() {
        let xhr = new XMLHttpRequest()
        xhr.open("get", "http://localhost:8080/get", true)
        xhr.send()
        xhr.onreadystatechange = () => {
            if (xhr.readyState === 4 && xhr.status === 200) {
                console.log(xhr.responseText)
                let resp = JSON.parse(xhr.responseText)
                console.log(resp.msg)
            }
        }
    }

    function send4() {
        fetch("http://localhost:8080/get").then(res => {
            console.log(res)
            return res.json()  // 将响应结果转换成json对象
        }).then(resp => {
            console.log(resp)
        }).catch(err => {
            console.log(err)
        })
    }
</script>
</body>
</html>